const exec = require('child_process').execSync;

function pushCode() {
  const p1 = new Promise((resolve, reject) => {
    exec('git pull', { stdio: 'inherit'}, err => {
      if (err) reject(err)
      else resolve()
    })
  })
  const p2 = new Promise((resolve, reject) => {
    exec('git add .', { stdio: 'inherit'}, err => {
      if (err) reject(err)
      else resolve()
    })
  })
  const p3 = new Promise((resolve, reject) => {
    exec(`git commit -m '${process.argv[2]}'`, { stdio: 'inherit' }, err => {
      if (err) reject(err)
      else resolve()
    })
  })
  const p4= new Promise((resolve, reject) => {
    exec(`git push origin master`, { stdio: 'inherit' }, err => {
      if (err) reject(err)
      else resolve()
    })
  })
  
  p1
    .then(() => {
      return p2
    })
    .then(() => {
      return p3
    })
    .then(() => {
      return p4
    })
}

pushCode()